package Log;

import utils.FileUtil;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TxtLog implements ISysLog{

//    2021-8-28
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
//    3:33 下午
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date now = new Date();
//    日志文件地址
    String destFilePath = "log/" + sdf1.format(now) + ".txt";


    @Override
    public void info(String msg) {
        //1.封装日志对象
        String log = new Log(msg,INFO,sdf2.format(now)) + "\r\n";
        //日志数据写入文件
        try {
            FileUtil.writeByBuffered(log,destFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void warn(String msg) {
        //1.封装日志对象
        String log = new Log(msg,WARN,sdf2.format(now)) + "\r\n";
        //日志数据写入文件
        try {
            FileUtil.writeByBuffered(log,destFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void error(String msg) {
        //1.封装日志对象
        String log = new Log(msg,ERROR,sdf2.format(now)) + "\r\n";
        //日志数据写入文件
        try {
            FileUtil.writeByBuffered(log,destFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
